package org.truenewx.tnxjee.repo.mybatis.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.truenewx.tnxjee.core.util.CollectionUtil;
import org.truenewx.tnxjee.model.entity.Entity;
import org.truenewx.tnxjee.model.query.QueryIgnoring;
import org.truenewx.tnxjee.model.query.QueryResult;
import org.truenewx.tnxjee.model.query.Querying;
import org.truenewx.tnxjee.repo.mybatis.model.query.QueryResultPage;

public interface EntityMapper<T extends Entity> extends BaseMapper<T> {

    int save(T unity);

    default QueryResult<T> doQuery(Querying querying, Wrapper<T> queryWrapper) {
        QueryResultPage<T> page = new QueryResultPage<>(querying);
        selectPage(page, queryWrapper);
        return page;
    }

    default QueryResult<T> doQuery(int pageSize, int pageNo, boolean countable, Wrapper<T> queryWrapper) {
        Querying querying = new Querying(pageSize, pageNo);
        querying.setIgnoring(countable ? null : QueryIgnoring.TOTAL);
        return doQuery(querying, queryWrapper);
    }

    default T selectFirst(Wrapper<T> queryWrapper) {
        QueryResult<T> result = doQuery(1, 1, false, queryWrapper);
        return CollectionUtil.getFirst(result);
    }

}
